﻿using ApiManage.Models.zyhModels;
using ApiManage.Service.MsSql.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace ApiManage.Service.MsSql.Service
{
    public class WorkInterfaceService : BaseService, IWorkInterfaceService
    {

        public List<WorkInterface> GetList(Expression<Func<WorkInterface, bool>> where) => _zyhContext.WorkInterface.Where(where).ToList();

        public void Insert(WorkInterface param)
        {
            param.RequestHeader = string.IsNullOrEmpty(param.RequestHeader) ? "" : param.RequestHeader.Replace("\n", "");
            param.RequestOther = string.IsNullOrEmpty(param.RequestOther) ? "" : param.RequestOther.Replace("\n", "");
            param.ResponseParam = string.IsNullOrEmpty(param.ResponseParam) ? "" : param.ResponseParam.Replace("\n", "");
            param.ResponseParamDesc = string.IsNullOrEmpty(param.ResponseParamDesc) ? "" : param.ResponseParamDesc.Replace("\n", "");
            param.RequestParam = string.IsNullOrEmpty(param.RequestParam) ? "" : param.RequestParam.Replace("\n", "");
            param.RequestParamDesc = string.IsNullOrEmpty(param.RequestParamDesc) ? "" : param.RequestParamDesc.Replace("\n", "");
            param.ResponseSuccExam = string.IsNullOrEmpty(param.ResponseSuccExam) ? "" : param.ResponseSuccExam.Replace("\n", "");
            param.ResponseErrExam = string.IsNullOrEmpty(param.ResponseErrExam) ? "" : param.ResponseErrExam.Replace("\n", "");
            _zyhContext.Add(param);
            _zyhContext.SaveChanges();
        }

        public void Update(WorkInterface param)
        {
            var item = _zyhContext.WorkInterface.FirstOrDefault(o => o.Id == param.Id);
            item.ModuleId = param.ModuleId;
            item.Name = param.Name;
            item.Description = param.Description;
            item.Url = param.Url;
            item.Method = param.Method;
            item.Version = param.Version;
            item.RequestHeader = string.IsNullOrEmpty(param.RequestHeader) ? "" : param.RequestHeader;
            item.RequestOther = string.IsNullOrEmpty(param.RequestOther) ? "" : param.RequestOther;
            item.RequestContentType = param.RequestContentType;
            item.RequestParam = string.IsNullOrEmpty(param.RequestParam) ? "" : param.RequestParam;
            item.RequestParamDesc = string.IsNullOrEmpty(param.RequestParamDesc) ? "" : param.RequestParamDesc;
            item.ResponseContentType = param.ResponseContentType;
            item.ResponseParam = string.IsNullOrEmpty(param.ResponseParam) ? "" : param.ResponseParam;
            item.ResponseParamDesc = string.IsNullOrEmpty(param.ResponseParamDesc) ? "" : param.ResponseParamDesc;
            item.ResponseSuccExam = string.IsNullOrEmpty(param.ResponseSuccExam) ? "" : param.ResponseSuccExam;
            item.ResponseErrExam = string.IsNullOrEmpty(param.ResponseErrExam) ? "" : param.ResponseErrExam;
            item.Sort = param.Sort;
            item.UseJwt = param.UseJwt;
            _zyhContext.SaveChanges();
        }

        public void Delete(int id)
        {
            var User = _zyhContext.WorkInterface.FirstOrDefault(o => o.Id == id);
            User.Status = 9;
            _zyhContext.SaveChanges();
        }

        public WorkInterface GetInfo(int id) => _zyhContext.WorkInterface.FirstOrDefault(o => o.Id == id);
    }
}
